package vmcs.simulationsystem;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto, Pang Pingli
//

import java.awt.*;
import java.awt.event.*;

import vmcs.MoneyFormatter;
import vmcs.common.Constant;
import vmcs.common.VMCSException;
import vmcs.controller.MaintenanceController;
import vmcs.store.Store;
import vmcs.store.StoreController;
import vmcs.ui.interfacecontrol.IMaintenancePanel;
import vmcs.ui.interfacecontrol.ISimulatorPanel;

public class MaintenanceSimulationPanel implements IMaintenancePanel {

    private LabelledDisplay  password;
    private LabelledDisplay  collectCash;
    private Button           exitBtn;
    private CoinDisplay      cDisplay; // need to be access from other class.
    private DrinkDisplay     dDisplay;  // need to be access from other class.
    private ButtonItem       totalCash;
    private Button           transferCash;
    private WarningDisplay   validPswd;
    private WarningDisplay   invalidPswd;
	private Dialog dialog;

    public MaintenanceSimulationPanel () {   
    }
    
    public LabelledDisplay getPassword() {
		return password;
	}

	public void setPassword(LabelledDisplay password) {
		this.password = password;
	}

	public LabelledDisplay getCollectCash() {
		return collectCash;
	}

	public void setCollectCash(LabelledDisplay collectCash) {
		this.collectCash = collectCash;
	}

	public Button getExitBtn() {
		return exitBtn;
	}

	public void setExitBtn(Button exitBtn) {
		this.exitBtn = exitBtn;
	}

	public CoinDisplay getcDisplay() {
		return cDisplay;
	}

	public void setcDisplay(CoinDisplay cDisplay) {
		this.cDisplay = cDisplay;
	}

	public DrinkDisplay getdDisplay() {
		return dDisplay;
	}

	public void setdDisplay(DrinkDisplay dDisplay) {
		this.dDisplay = dDisplay;
	}

	public ButtonItem getTotalCash() {
		return totalCash;
	}

	public void setTotalCash(ButtonItem totalCash) {
		this.totalCash = totalCash;
	}

	public Button getTransferCash() {
		return transferCash;
	}

	public void setTransferCash(Button transferCash) {
		this.transferCash = transferCash;
	}

	public WarningDisplay getValidPswd() {
		return validPswd;
	}

	public void setValidPswd(WarningDisplay validPswd) {
		this.validPswd = validPswd;
	}

	public WarningDisplay getInvalidPswd() {
		return invalidPswd;
	}

	public void setInvalidPswd(WarningDisplay invalidPswd) {
		this.invalidPswd = invalidPswd;
	}

	public Dialog getDialog() {
		return dialog;
	}

	public void setDialog(Dialog dialog) {
		this.dialog = dialog;
	}

    @Override
    public void  display(){
       System.out.println("MaintenancePanel: before display");
       dialog.setVisible(true);
      System.out.println("MaintenancePanel: after display");

    }

    @Override
    public void closeDown () {
    	dialog.dispose ();
    }

    public void setCoinDisplayQuantity(int idx, int qty) throws VMCSException{
      cDisplay.displayQty(idx, qty);
    }

    public void setDrinkDisplayQuantity(int idx, int qty) throws VMCSException{
      dDisplay.displayQty(idx,qty);
    }


    public void displayPasswordState(boolean st){
      if(st == true){
        validPswd.setState(true);
        invalidPswd.setState(false);
      }
      else {
        validPswd.setState(false);
        invalidPswd.setState(true);
      }

    }

    private void setActive(char comp, boolean st){
      switch (comp){
        case Constant.DIALOG:
           setActive(Constant.PSWD, st);
           setActive(Constant.WORKING, !st);
          validPswd.setState(false);
          invalidPswd.setState(false);
          break;
        case Constant.WORKING:
             collectCash.setActive(st);
             cDisplay.setActive(st);
             dDisplay.setActive(st);
             totalCash.setActive(st);
             transferCash.setEnabled(st);
             exitBtn.setEnabled(st);
             break;
        case Constant.PSWD:
             password.setActive(st);
             break;
    }
  }

  public int  getCurIdx(){
    return dDisplay.getCurIdx();
  }

  public void displayTotalCash(int tc){
    String  stc;

    stc = MoneyFormatter.toMoney(tc);
    totalCash.setValue(stc);
  }

  public void displayCoins(int cc){
    collectCash.setValue(cc);
  }

  /**
   *  use when machinery simulator panel changes qty;
   *  It is used to automatically update the displayed quantity in maintenance panel.
   *  It is called by Maintenance Controller
   *  Not required in requirement.
   */

   public void updateQtyDisplay(int type, int idx, int qty)
  throws VMCSException {
    if(type == Store.CASH) {
      if(cDisplay.getCurIdx() == idx)
        cDisplay.displayQty(idx, qty);
    }
    else {
      if(dDisplay.getCurIdx() == idx)
        dDisplay.displayQty(idx, qty);
    }
  }

  /**
   * When transfer all button is pushed, the current display needs to be updated.
   * not required in requirement.
   */
  public void updateCurrentQtyDisplay(int type, int qty)
  throws VMCSException {
    int curIdx;
    if(type == Store.CASH)
      curIdx = cDisplay.getCurIdx();
    else
      curIdx = dDisplay.getCurIdx();
    updateQtyDisplay(type, curIdx, qty);
  }


  public void initCollectCash(){
    collectCash.setValue("");
  }

  public void initTotalCash(){
    totalCash.setValue("");
  }

  public void clearPassword(){
    password.setValue("");
  }

  public void displayPrice(int price){
   dDisplay.getPriceDisplay().setValue(MoneyFormatter.toMoney(price));
  }

	@Override
	public void setMaintenancePanelActive(char comp, boolean state) {
		this.setActive(comp, state);
	}

	@Override
	public void showMessage(String message) {
		MessageDialog msg =
	            new MessageDialog(dialog, message);
	    msg.setVisible(true);
		
	}

	@Override
	public void setPriceDisplayValue(String value) {
		dDisplay.getPriceDisplay().setValue(value);		
	}
}


